package com.qn.example.service.impl;

import com.qn.example.mapper.AddressBookMapper;
import com.qn.example.pojo.AddressBook;
import com.qn.example.service.AddressBookService;
import com.qn.example.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/19 9:19
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookDao;

    @Override
    public void saveAddressBook(AddressBook addressBook) {
        //生成id
        Long id = UUIDUtils.getLongId();
        addressBook.setId(id);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        //调用dao的添加方法
        addressBookDao.saveAddressBook(addressBook);
    }

    @Override
    public List<AddressBook> addressList(Long userId) {

        return addressBookDao.addressList(userId);
    }

    @Override
    public void updateDefault(AddressBook addressBook, Long userId) {


        //现将该用户的所有地址改成非默认状态
        addressBookDao.removeDefaultAddress(userId);
        //再把点前地址改成默认
        addressBookDao.setDefaultAddress(addressBook);

    }

    @Override
    public AddressBook getAddressBookById(Long id) {

        return  addressBookDao.getAddressBookById(id);
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBookDao.updateAddressBook(addressBook);

    }

    @Override
    public void deleteAddressByIds(Long ids) {
        addressBookDao.deleteAddressByIds(ids);
    }

    @Override
    public AddressBook findDefaultAddressBook(Long userId) {
        return addressBookDao.findDefaultAddressBook(userId);
    }
}
